package com.dao.impl;

import com.bean.Flower;
import com.dao.FlowerDao;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

public class FlowerDaoImpl extends BaseDao implements FlowerDao {

    @Override
    public Integer queryForPageTotalCount() {
        String sql = "select count(*) from flower_detail";
        Number count = (Number) queryForSingleValue(sql);
        return count.intValue();
    }

    @Override
    public List<Flower> queryForPageItem(int begin, int pageSize) {
        String sql = "select `flowerid`,`flower_name`,`introduce`,`price`,`enterpriseid`,`fclassid`,`sales`,`stock`,`flower_photo` flower_photo from flower_detail limit ?,?";
        return queryForList(Flower.class, sql, begin, pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByPrice(int min, int max) {
        String sql = "select count(*) from flower_detail where price between ? and ?";
        Number count = (Number) queryForSingleValue(sql, min, max);
        return count.intValue();
    }

    @Override
    public List<Flower> queryForPageItemByPrice(int begin, int pageSize, int min, int max) {
        String sql = "select `flowerid`,`flower_name`,`introduce`,`price`,`enterpriseid`,`fclassid`,`sales`,`stock`,`flower_photo` flower_photo " +
                "from flower_detail where price between ? and ? order by price limit ?,?";
        return queryForList(Flower.class, sql, min, max, begin, pageSize);
    }

    @Override
    public Integer queryForPageTotalCountByName(String flower_name) {
        String sql = "select count(*) from flower_detail where flower_name like ?";
        Number count = (Number) queryForSingleValue(sql, "%" + flower_name + "%");
        return count.intValue();
    }

    @Override
    public List<Flower> queryForPageItemByName(int begin, int pageSize, String flower_name) {
        String sql = "select `flowerid`,`flower_name`,`introduce`,`price`,`enterpriseid`,`sales`,`stock`,`fclassid`,`flower_photo` flower_photo " +
                "from flower_detail where flower_name like ? limit ?,?";
        return queryForList(Flower.class, sql, "%" + flower_name + "%", begin, pageSize);
    }

    @Override
    public Flower queryFlowerByFlowerid(int id) {
        String sql = "select `flowerid`, `flower_name`, `introduce`, `price`,`enterpriseid`,`fclassid`,`sales`,`stock`,`flower_photo` flower_photo from flower_detail where flowerid=? ";
        return queryForOne(Flower.class, sql, id);
    }

    @Override
    public Integer queryForPageTotalCountByEnterpriseid(String enterpriseid) {
        String sql = "select count(*) from flower_detail where enterpriseid=?";
        Number count = (Number) queryForSingleValue(sql, enterpriseid);
        return count.intValue();
    }

    @Override
    public List<Flower> queryForPageItemByEnterpriseid(int begin, int pageSize, String enterpriseid) {
        String sql = "select `flowerid`,`flower_name`,`introduce`,`price`,`enterpriseid`,`fclassid`,`sales`,`stock`,`flower_photo` flower_photo " +
                "from flower_detail where enterpriseid=? limit ?,?";
        return queryForList(Flower.class, sql, enterpriseid, begin, pageSize);
    }

    @Override
    public int updateFlower(Flower flower) {
        String sql = "update flower_detail set `flower_name`=?,`price`=?, `introduce`=?,`flower_photo`=?,`fclassid`=?,`enterpriseid`=? where flowerid=?";
        return update(sql, flower.getFlower_name(), flower.getPrice(), flower.getIntroduce(), flower.getFlower_photo(), flower.getFclassid(), flower.getEnterpriseid(), flower.getFlowerid());
    }

    @Override
    public int addFlower(Flower flower) {
        String sql = "insert into flower_detail(`flower_name`,`price`, `introduce`,`flower_photo`,`fclassid`,`enterpriseid`)values(?,?,?,?,?,?)";
        return update(sql, flower.getFlower_name(), flower.getPrice(), flower.getIntroduce(), flower.getFlower_photo(), flower.getFclassid(), flower.getEnterpriseid());
    }

    @Override
    public int deleteFlowerByFlowerid(int flowerid) {
        String sql = "delete from flower_detail where flowerid=?";
        return update(sql, flowerid);
    }

    @Override
    public Integer queryForPageTotalCountByFclassid(Integer fclassid) {

        String sql = "select count(*) from flower_detail where fclassid=?";
        Number count = (Number) queryForSingleValue(sql, fclassid);
        return count.intValue();
    }

    @Override
    public List<Flower> queryForPageItemByFclassid(int begin, int pageSize, Integer fclassid) {
        String sql = "select `flowerid`,`flower_name`,`introduce`,`price`,`enterpriseid`,`fclassid`,`sales`,`stock`,`flower_photo` flower_photo " +
                "from flower_detail where fclassid=? limit ?,?";
        return queryForList(Flower.class, sql, fclassid, begin, pageSize);
    }

    @Override
    public int updateFlowerByFlower(Integer sales, Integer stock, Integer flowerid) {
        String sql = "update flower_detail set `sales`=?,`stock`=? where flowerid=?";
        return update(sql, sales, stock, flowerid);
    }

    @Override
    public List<Flower> queryBySales() {
        String sql = "select `flowerid`, `flower_name`, `introduce`, `price`,`enterpriseid`,`fclassid`,`sales`,`stock`,`flower_photo` flower_photo from flower_detail order by sales desc";

        return queryForList(Flower.class, sql);
    }

    @Override
    public List<Map<String, Object>> listquery(int flowerid) {
        String sql = "select * from flower_detail,flower_classify,enterprise where flowerid=? and flower_detail.fclassid = flower_classify.fclassid and flower_detail.enterpriseid=enterprise.enterpriseid";
        return listquery(Flower.class, sql, flowerid);
    }

    @Override
    public int updateById(Integer flowerid, String flower_name, BigDecimal price, String introduce, Integer stock, Integer fclassid) {
        String sql = "update flower_detail set `flower_name`=?,`price`=?, `introduce`=?,`stock`=?,`fclassid`=? where flowerid=?";
        return update(sql, flower_name, price, introduce, stock, fclassid, flowerid);
    }

    @Override
    public int insertFlower(String flower_name, BigDecimal price, String introduce, Integer stock, Integer fclassid, String enterpriseid, String flower_phoro, Integer sales) {
        String sql = "insert into flower_detail(`flower_name`,`price`, `introduce`,`flower_photo`,`stock`,`fclassid`,`enterpriseid`,`sales`)values(?,?,?,?,?,?,?,?)";
        return update(sql, flower_name, price, introduce, flower_phoro, stock, fclassid, enterpriseid, sales);
    }

    @Override
    public int updatePhoto(Integer flowerid, String enterpriseid, String flower_photo) {
        String sql = "update flower_detail set `flower_photo`=? where flowerid=? and enterpriseid=?";
        return update(sql, flower_photo, flowerid, enterpriseid);
    }

    @Override
    public List<Map<String, Object>> queryByName(String content) {
        String sql = "select  * from flower_detail,enterprise where flower_detail.flower_name like ? or enterprise.enterprise_name like ? group by flowerid";
        return listquery(Flower.class,sql,"%"+content+"%","%"+content+"%");
    }

    @Override
    public List<Flower> queryByPrice(BigDecimal price) {
        String sql = "select `flowerid`, `flower_name`, `introduce`, `price`,`enterpriseid`,`fclassid`,`sales`,`stock`,`flower_photo` flower_photo from flower_detail where price<=? order by price";
        return queryForList(Flower.class,sql,price);
    }
}
